package com.imy.luascript.utils;

import android.content.Context;
import android.util.Log;

import com.imy.common.util.ALog;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;

/**
 * create by Imy
 */
public class AssetsUtils {


  /**
   * 读取文本数据
   *
   * @param context  程序上下文
   * @param fileName 文件名
   * @return String, 读取到的文本内容，失败返回null
   */
  public static String readAssets(Context context, String fileName) {
    InputStream is = null;
    String content = null;
    try {
      is = context.getAssets().open(fileName);

      byte[] buffer = new byte[1024];
      ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
      while (true) {
        int readLength = is.read(buffer);
        if (readLength == -1)
          break;
        arrayOutputStream.write(buffer, 0, readLength);
      }
      is.close();
      arrayOutputStream.close();
      content = new String(arrayOutputStream.toByteArray());

    } catch (FileNotFoundException e) {
      ALog.e("FileNotFoundException:"+e.getMessage());
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
      content = null;
    } finally {
      try {
        if (is != null)
          is.close();
      } catch (IOException ioe) {
        ioe.printStackTrace();
      }
    }
    return content;
  }
}
